package edu.hunau.cxb19.oop.class19;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import edu.hunau.cxb19.oop.class19.pojo.Apple;

/**
 *
 * @author hb
 *
 *         2020-03-20 14:50:59
 */
public class ArrayListTest {
	List<Apple> datas = null;

	@Before // junit提供的一个注解，能够确保before标注的方法在test方法执行之前执行
	public void initDatas() {
		datas = new ArrayList<Apple>();
		datas.add(new Apple("red", 150, "山西"));
		datas.add(new Apple("red", 110, "山西"));
		datas.add(new Apple("green", 150, "山东"));
		datas.add(new Apple("red", 100, "山东"));
		datas.add(null);
		datas.add(new Apple("green", 170, "新疆"));
		datas.add(new Apple("green", 100, "新疆"));
		datas.add(new Apple("red", 120, "新疆"));
	}

	@Test
	public void iteratorTest() {
		// 获得一个迭代器
		Iterator<Apple> iterator = datas.iterator();// 由Collection接口提供
		while (iterator.hasNext()) {// 判定迭代器中是否有数据
			Apple apple = iterator.next();
			System.out.println(apple);
		}
	}

	@Test
	public void forTest() {
		for (int i = 0; i < datas.size(); i++) {
			Apple apple = datas.get(i);
			System.out.println(apple);
		}
	}

	@Test
	public void forEachTest() {
		for (Apple apple : datas) {
			System.out.println(apple);
		}
	}

	@Test
	public void findRedApple() {
		List<Apple> redApples = filterAppleByColor("red");
		System.out.println(redApples);
	}

	/**
	 * 根据颜色过滤苹果
	 * 
	 * @return
	 */
	public List<Apple> filterAppleByColor(String color) {
		List<Apple> result = new ArrayList<Apple>();
		for (Apple apple : datas) {
			if (apple.getColor().equals(color)) {
				result.add(apple);
			}
		}
		return result;
	}

	@Test
	public void containsTest() {
		// 判定集合中是否存在150克的山西红苹果
		Apple apple = new Apple("red", 150, "山西");
		System.out.println(datas.contains(apple));
	}
}
